#!/bin/zsh

# TARGETS
# Shows historical in plot

USR=$1
EXERCISE=$2
checkvars USR EXERCISE

set -eu

DIR=$( cd $( dirname $0 ) ; /bin/pwd )
[[ -d users/${USR} ]] || crash "No such user: ${USER}"

NAMES_FILE=users/${USR}/settings/names-weights.txt
EXERCISE_NAME=( $( grep "^${EXERCISE} " ${NAMES_FILE} ) )
shift EXERCISE_NAME

export TITLE="${(j: :)EXERCISE_NAME}"
d TITLE

make_temp()
{
  mktemp --tmpdir=${PWD} x-smack.tmp.XXX
}

export TMPDIR=
FILE=$( make_temp )

if ! ${DIR}/targets.zsh ${*} > ${FILE}
then
  declare FILE
  cat ${FILE}
  exit 1
fi
DATA=$( make_temp )
while read -A A
do
  DATE=$( date --date=${A[1]} +%s000 )
  T=${A[2]}
  TARGET=${T#target=}
  print ${DATE} ${TARGET} >> ${DATA}
done < ${FILE}

EPS=targets.eps
CFG_TEMPLATE=${DIR}/targets.cfg.m4
CFG=$( make_temp )
m4 ${CFG_TEMPLATE} > ${CFG}
set -x
swift_plotter.zsh -s ${CFG:t} ${EPS:t} ${DATA:t}

rm ${FILE} ${CFG} ${DATA}
